//
//  MOAppraiseViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/10/18.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAppraiseViewController.h"

@interface MOAppraiseViewController ()

@property(nonatomic,strong)UIView *navView;

@property(nonatomic,strong)UIView *contentBackView;

@property(nonatomic,strong)UILabel *canTingNameLabel;

@property(nonatomic,strong)UILabel *foodNameLabel;

@property(nonatomic,strong)NSMutableArray *buttonArray;

@property(nonatomic,strong)UITextView *commentTextView;

@property(nonatomic,strong)UIButton *submitButton;

@property(nonatomic,assign)NSInteger star;

@end

@implementation MOAppraiseViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = KGrayColor_f8f8f8;
    
    [self.view addSubview:self.navView];
    
    [self.view addSubview:self.contentBackView];
    
    [self.view addSubview:self.submitButton];
    
    if ([self.isDetail integerValue]==1) {
        self.commentTextView.text = self.content;
        self.commentTextView.placeholder = @"";
        
        self.submitButton.hidden = YES;
        
        for (int i = 0; i<self.buttonArray.count; i++) {
            UIButton *button = self.buttonArray[i];
            if (i<[self.starValue integerValue]) {
                [button setBackgroundImage:IMAGE(@"xing_selected") forState:UIControlStateNormal];
                [button setBackgroundImage:IMAGE(@"xing_selected") forState:UIControlStateHighlighted];
            }else{
                [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateNormal];
                [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateHighlighted];
            }
        }
    }
}


-(void)clickBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clickButton:(UIButton *)sender
{
    if ([self.isDetail integerValue]==1) {
        return;
    }
    
    self.star = sender.tag+1;
    for (int i = 0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        if (i<=sender.tag) {
            [button setBackgroundImage:IMAGE(@"xing_selected") forState:UIControlStateNormal];
            [button setBackgroundImage:IMAGE(@"xing_selected") forState:UIControlStateHighlighted];
        }else{
            [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateNormal];
            [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateHighlighted];
        }
    }
}

-(void)submitButtonClick
{
    if (self.star<1) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请选择评价星级!" showInView:self.view];
        return;
    }
    
    if (self.commentTextView.text.length==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请填写评价！" showInView:self.view];
        return;
    }
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ADDDISHRATE,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"restaurantId":self.restaurantId,
                          @"dishId":self.dishId,
                          @"appraisalUser":[MOUser sharedMOUser].userName,
                          @"star":[NSString stringWithFormat:@"%ld",(long)self.star],
                          @"content":self.commentTextView.text
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"菜品评价：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [[ADShowMessage shareShowMessage]showTextOnly:@"评价失败，稍后重试" showInView:self.view];
        }
    } failure:^(NSError *error) {
        NSLog(@"菜品评价：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark --懒加载
-(UIView *)navView
{
    if (!_navView) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, StatusBarAndNavigationBarHeight)];
        _navView.backgroundColor = KGrayColor_f8f8f8;
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 25, 25)];
        [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:IMAGE(@"nav_back") forState:UIControlStateNormal];
        backButton.centerY = StatusBarHeight + (StatusBarAndNavigationBarHeight - StatusBarHeight)/2;
        [_navView addSubview:backButton];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 32 - 100, 25)];
        titleLabel.text = @"餐品评价";
        titleLabel.font =  [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = KBlackColor_222222;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.center = CGPointMake(SCREEN_WIDTH/2, backButton.centerY);
        [_navView addSubview:titleLabel];
    }
    return _navView;
}

-(UIView *)contentBackView
{
    if (!_contentBackView) {
        _contentBackView = [[UIView alloc]initWithFrame:CGRectMake(10, _navView.bottom+10, SCREEN_WIDTH-20, 1)];
        _contentBackView.backgroundColor = [UIColor whiteColor];
        
        self.canTingNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, _contentBackView.width, 17)];
        self.canTingNameLabel.font = [UIFont boldSystemFontOfSize:16];
        self.canTingNameLabel.textColor = KBlackColor_2A2A2A;
        self.canTingNameLabel.textAlignment = NSTextAlignmentCenter;
        self.canTingNameLabel.text = self.restaurantName;
        [_contentBackView addSubview:self.canTingNameLabel];
        
        self.foodNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.canTingNameLabel.bottom+10, _contentBackView.width, 14)];
        self.foodNameLabel.font = FONT_SIZE(13);
        self.foodNameLabel.textColor = KBlackColor_2A2A2A;
        self.foodNameLabel.textAlignment = NSTextAlignmentCenter;
        NSString *type;
        if ([self.type integerValue]==1) {
            type = @"早餐";
        }else if ([self.type integerValue]==2){
            type = @"午餐";
        }else if ([self.type integerValue]==3){
            type = @"晚餐";
        }else if ([self.type integerValue]==4){
            type = @"其他";
        }else{
            type = @"";
        }
        if (type.length>0) {
            self.foodNameLabel.text = [NSString stringWithFormat:@"%@(%@)",self.dishName,type];
        }else{
            self.foodNameLabel.text = self.dishName;
        }
        
        [_contentBackView addSubview:self.foodNameLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20, self.foodNameLabel.bottom +20, _contentBackView.width - 40, 1)];
        line.backgroundColor = KGrayColor_ededed;
        [_contentBackView addSubview:line];
        
        UILabel *showLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, line.bottom + 20, _contentBackView.width, 17)];
        showLabel.font = [UIFont boldSystemFontOfSize:16];
        showLabel.textColor = KBlackColor_2A2A2A;
        showLabel.textAlignment = NSTextAlignmentCenter;
        showLabel.text = @"评价";
        [_contentBackView addSubview:showLabel];
        
        self.buttonArray = [NSMutableArray array];
        CGFloat buttonW = SCREEN_WIDTH/13;
        for (int i = 0; i<5; i++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((_contentBackView.width-buttonW*9)/2+buttonW*2*i, showLabel.bottom+20, buttonW, buttonW)];
            [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateNormal];
            [button setBackgroundImage:IMAGE(@"xing_normal") forState:UIControlStateHighlighted];
            button.tag = i;
            [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.buttonArray addObject:button];
            [_contentBackView addSubview:button];
        }
        
        UIView *commentBackView = [[UIView alloc]initWithFrame:CGRectMake(19, showLabel.bottom + 20 + buttonW + 20, _contentBackView.width-38, 104)];
        commentBackView.backgroundColor = [UIColor whiteColor];
        commentBackView.layer.masksToBounds = YES;
        commentBackView.layer.cornerRadius = 5.5;
        commentBackView.layer.borderWidth = 1;
        commentBackView.layer.borderColor = KGrayColor_ededed.CGColor;
        [_contentBackView addSubview:commentBackView];
        
        self.commentTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, commentBackView.width-20, commentBackView.height-20)];
        self.commentTextView.font = FONT_SIZE(13);
        self.commentTextView.textColor = KBlackColor_2A2A2A;
        self.commentTextView.placeholder = @"请输入评语";
        if ([self.isDetail integerValue]==1) {
            self.commentTextView.userInteractionEnabled = NO;
        }
        [commentBackView addSubview:self.commentTextView];
        
        _contentBackView.height = commentBackView.bottom + 20;

    }
    return _contentBackView;
}

-(UIButton *)submitButton
{
    if (!_submitButton) {
        _submitButton = [[UIButton alloc]initWithFrame:CGRectMake(35, _contentBackView.bottom +20, SCREEN_WIDTH - 70, 45)];
        [_submitButton setBackgroundColor:KBlueColor_2C94FF];
        [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
        _submitButton.layer.masksToBounds = YES;
        _submitButton.layer.cornerRadius = 3.0;
        [_submitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitButton;
}


@end
